package com.leyou.cart.mq;

import com.leyou.cart.service.CartService;
import com.leyou.common.constants.MQConstants;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.lang.annotation.ElementType;
import java.util.List;
import java.util.Map;

/**
 * @author xiuer
 * @description 监听订单微服务消息
 * @create 2019/7/14 18:30
 */
@Slf4j
@Component
public class ListenerOrderMQ {
    @Autowired
    private CartService cartService;

    //监听MQ，已下单商品删除
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MQConstants.Queue.ORDER_DELTET_GOODS_QUEUE, durable = "true"),
            exchange = @Exchange(
                    value = MQConstants.Exchange.ORDER_EXCHANGE_NAME, type = ExchangeTypes.TOPIC
            ),
            key = MQConstants.RoutingKey.ORDER_DELETE_KEY))
    public void listenerMQSkuIds(Map<String, Object> MQmaps) {
        try {
            if (MQmaps == null) {
                log.error("【购物车微服务】消息接收失败");
                throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
            }
            log.info("【购物车微服务】消息接收成功 {}", MQmaps.toString());
            cartService.deleteCartBySkuIds(MQmaps);
        } catch (Exception e) {
            log.error("【购物车微服务】消息接收失败 原因 {}", e.getMessage(), e);
            throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
    }

}
